package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-13
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    //注入service
    @Autowired
    EduTeacherService teacherService;

    @ApiOperation(value = "获取所有讲师列表")
    @GetMapping(value="/findAll")
    public R findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);

    }
    //2、逻辑删除讲师
    @ApiOperation(value = "根据id逻辑删除讲师信息")
    @DeleteMapping(value = "/{id}")
    public R removeTeacher(
            @ApiParam(value = "讲师ID",name = "id",required = true)
            @PathVariable("id")String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }

    }

    //3、分页查询讲师的方法
    @GetMapping(value = "/pageTeacher/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页查询讲师信息")
    public R pageListTeacher(
            @PathVariable(value = "pageNum") Long pageNum,
            @PathVariable(value = "pageSize")Long pageSize){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<EduTeacher>(pageNum,pageSize) ;
        //调用方法实现分页
        //调用方法时候
        try {
            int i = 10/0;

        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(1001,"不正确输入");
        }
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
//        return R.ok().data("total",total).data("rows",records);
        return R.ok().data(map);
    }
    //4 条件查询
    @PostMapping(value = "/pageTeacherCondition/{pageNum}/{pageSize}")
    @ApiOperation(value = "带条件分页查询讲师信息")
    public R pageTeacherCondition(@PathVariable("pageNum") Integer pageNum,
                                  @PathVariable("pageSize") Integer pageSize,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
       Page<EduTeacher> pageTeacher = new Page<>(pageNum,pageSize);
       //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<EduTeacher>();
        //mybatis动态sql  --》wrapper
        //判断条件值是否为空，如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件是否为空
        if(!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("begin",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("end",end  );
        }

        //排序
        wrapper.orderByDesc("gmt_create");


        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }
    //添加讲师的接口
    @ApiOperation(value = "添加讲师信息")
    @PostMapping(value = "/addTeacher")
    public R addTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        if(flag){

            return R.ok();
        }else{
            return R.error();
        }
    }
    //根据讲师id进行查询
    @ApiOperation(value = "根据id查询讲师信息")
    @GetMapping(value = "/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id){
        EduTeacher teacher = teacherService.getById(id);

        return R.ok().data("teacher",teacher);
    }
    //修改讲师信息
    @ApiOperation(value = "根据id修改讲师信息")
    @PostMapping(value="/updateTeacherById")
    public R updateTeacherById(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

